package DAO.impl;

import java.util.Date;

import model.impl.Passenger;

import org.hibernate.Query;

import DAO.PassengerDAO;

/**
 *  Implements PassengerDAO interface, extend GenericDAOImpl class using as 
 *  parameters entity of Passenger and Integer class as an ID. It also adds some 
 *  specific to Passenger entity methods
 */
public class PassengerDAOImpl extends GenericDAOImpl<Passenger, Integer>
		implements PassengerDAO {

	/**
	 * Looks for a Passenger with the passenger's instances equals to the 
	 * given parameters
	 * 
	 * @param name 		- Passenger.passengerName
	 * @param surname	- Passenger.passengerSurname
	 * @param data		- Passenger.passengerDateOfBirth
	 * @return			- Passenger entity object or null if such object was
	 * 				 	  not found
	 */
	public Passenger findPassengerByNameData(String name, String surname,
			Date data) {
		Passenger passenger = null;

		String sqlRequest = "SELECT p FROM Passenger p WHERE p.passengerName = :name " +
				"AND p.passengerSurname = :surname AND p.passengerDateOfBirth = :data";
		Query query = this.getSession().createQuery(sqlRequest)
				.setParameter("name", name).setParameter("surname", surname)
				.setParameter("data", data);
		passenger = findOne(query);

		return passenger;
	}

	/**
	 * Looks for a Passenger with the passenger's instances equals to the 
	 * given parameters
	 * 
	 * @param login	- Passenger.login
	 * @return		- Passenger entity object or null if such object was
	 * 				  not found
	 */
	public Passenger findPassengerByLogin(String login) {
		Passenger passenger = null;

		String sqlRequest = "SELECT p FROM Passenger p WHERE p.login = :log";
		Query query = this.getSession().createQuery(sqlRequest)
				.setParameter("log", login);
		passenger = findOne(query);

		return passenger;
	}

	/**
	 * Looks for a Passenger with the passenger's instances equals to the 
	 * given parameters
	 * 
	 * @param login		- Passenger.login
	 * @param password	- Passenger.password
	 * @return			- Passenger entity object or null if such object was
	 * 				 	  not found
	 */
	public Passenger findPassengerByLoginPassword(String login,
			String password) {
		Passenger passenger = null;

		String sqlRequest = "SELECT p FROM Passenger p WHERE p.login = :log " +
				"AND p.password = :pas";
		Query query = this.getSession().createQuery(sqlRequest)
				.setParameter("log", login).setParameter("pas", password);
		passenger = findOne(query);

		return passenger;
	}
}
